//
//  XCTabBar.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/6.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

class XCTabBar: UITabBar {
    
    var  addButton : UIButton?
    
    //在初始化时 添加addBtn
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(addBtn)
        
        addButton = addBtn
        
        //设置全局颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //布局tabBar上的子控件
    override func layoutSubviews() {
        //调用父类
        super.layoutSubviews()
        
        //定义tabBar子控件的frame
        let x : CGFloat = 0
        let y : CGFloat = 0
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(x, y, w, h)
        
        //定义一个计数器
        var count:CGFloat = 0
        for subuview in subviews {
            
            if subuview .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                
                //如果subview是UITabBarButton的话  那么自动布局
                 subuview.frame = CGRectOffset(rect, w * count, 0)
                
                if count == 1 {
                    count++
                    
                    //设置btn的frame
//                    x = w * count
//                    
                    addBtn.frame = CGRectOffset(rect, w * count, 0)

                }
                
                
                
                count++
//                x = w * count
            }
            
        }
    }
    
    
    //懒加载tabBar中间的  +
    
    lazy var addBtn:UIButton = {
        
        let btn = UIButton()
        
        //设置按钮属性
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        //图片自适应
//        btn.sizeToFit()

        return btn
    }()

}
